<?php
add_action('wp_head', 'submit_coupon');
function submit_coupon(){ ?>
<script type="text/javascript">
    jQuery(document).ready(function($) {

        function back_to_top(){
            var duration = 500;
                jQuery('html, body').animate({
                scrollTop: 0}, duration);
        }
        var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
            $('input[name="store_name"]').autocomplete({
                source: "auto-complete",
                minLength: 2
            });
        $('select[name="select_type"]').change(function(){
            var selectedValue = $('select[name="select_type"]').find(":selected").val();
            var coupon_code = $('input[name="coupon_code"]');
            var div = $('div.code_field');
            if(selectedValue == 'code' || selectedValue == 'free_shipping'){
                div.empty();
                var html = '<input class="entercode" type="text" placeholder="Enter Coupon code"  autocomplete="off" name="coupon_code" value="<?php echo $_POST['coupon_code'];?>" />';
                html +='<div class="warming hiden code-missing">*Enter coupon code</div>';
                div.append(html);
            }
            if(selectedValue == 'promotion'){
                div.empty();
            }
        });

        var frm = $('#coupon_submit');
        frm.submit(function(event){
           event.preventDefault();
           $('div.loading-submit-coupon').removeClass('hidden');
//           var duration = 500;
//           jQuery('html, body').animate({
//                scrollTop: 0
//            }, duration);
           $('input[name="submit_coupon"]').attr('disabled','disabled');
           var coupon_type = $('select[name="select_type"]').val();
           var store_url = $('input[name="store_name"]').val();
           var code='';
           if(coupon_type == 'promotion'){
               code = '';
           }else{
               code = $('input[name="coupon_code"]').val();
           }
           var description = $('textarea[name="des_cp"]').val();
           var expri_date = $('input[name="ex_date"]').datepicker({ dateFormat: 'yy/mm/dd' }).val();
           var security = $('input[name="6_letters_code"]').val();
           var title_coupon = $('input[name="title_coupon"]').val();
           var event_cp = $('select[name="select_event"]').val();
           var value_sending = [store_url,code,description,expri_date,security,coupon_type,title_coupon,event_cp];
           var data ={
                    action : 'submit_coupon',
                    value_send : value_sending
                };
           jQuery.post(ajaxurl, data, function(result) {
                    if(result == 'captcha fail'){
                        $('div.loading-submit-coupon').addClass('hidden');
                        $('input[name="submit_coupon"]').removeAttr('disabled');
                        $('div.captcha-fail').removeClass('hiden');
                    }
                    if(result == 'store fail'){
                        $('div.domain-fail').removeClass('hiden');
                        $('div.loading-submit-coupon').addClass('hidden');
                        $('input[name="submit_coupon"]').removeAttr('disabled');
                    }
                    if(result == 'submit success'){
                        $('div.loading-submit-coupon').addClass('hidden');
                        $('input[name="submit_coupon"]').removeAttr('disabled');
                        $('.warning').addClass('hidden');
                        //clear input text
                        $('input[name="store_name"]').val('');
                        $('input[name="coupon_code"]').val('');
                        $('textarea[name="des_cp"]').val('');
                        $('input[name="ex_date"]').val('');
                        $('input[name="title_coupon"]').val('');
                        $('input[name="6_letters_code"]').val('');
                        //success
                        $('.submit-success').removeClass('hidden');
                        var duration = 500;
                        jQuery('html, body').animate({
                             scrollTop: 0
                         }, duration);
                    }
                });
        });
    });
</script>
<?php }
add_action('wp_ajax_nopriv_submit_coupon', 'submit_coupon_callback');
add_action('wp_ajax_submit_coupon', 'submit_coupon_callback');
function submit_coupon_callback(){
    session_start();
    global $wpdb;
    $value_query = $_POST['value_send'];
    $store_url = $value_query[0];
    $code = $value_query[1];
    $description = $value_query[2];
    $expri_date = $value_query[3];
    $security = $value_query[4];
    $type_coupon = $value_query[5];
    $title = $value_query[6];
    $event = $value_query[7];
    if($_SESSION['6_letters_code'] == $security){
        $my_query ="
            SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key ='url_store_metadata' AND meta_value LIKE '%$store_url'
        ";
        $result = $wpdb->get_results($my_query);
        $store_id = $result[0]->post_id;
            if($store_id != 0)
            {
                $new_coupon = array(
                            'post_title' => $title,
                            'post_type' =>'coupon',
                            'post_status'=>'draft',
                            'post_content' => $description,
                            'post_author' => 1
                        );
                        $id = wp_insert_post($new_coupon,true);
                        if($id > 0)
                        {
                            add_post_meta($id,'submit_by_user',1,true);
                            add_post_meta($id, store_coupon_metadata, $store_id);
                            add_post_meta($id, coupon_type_metadata, $type_coupon);
                            add_post_meta($id, expire_date_metadata, $expri_date);
                            if($type_coupon == 'code' || $type_coupon == 'free_shipping')
                                add_post_meta($id, coupon_code_metadata, $code);
                        }
                        if($event != -1){
                            wp_set_post_terms( $id, $event, 'event',true );
                        }
						else{
							$event = '';
						}
                // send alert email
                $email_receive = get_option('email_new_coupon');
                $arr_mails = explode(',',$email_receive);

                $em_title = "New coupon submitted for: ".get_the_title($store_id)." | ".$title;
                $em_content = "Store URL: ".home_url("/?p=".$store_id)."<br/>".'URL: '.$store_url."<br/>".'Type: '.$type_coupon."<br/>".'Code: '.$code."<br/>Description: ".$description."<br/>Expire: ".$expri_date."<br/>Event: ".$event."<br/>Edit coupon: ".admin_url('/post.php?post='.$id.'&action=edit');
                foreach ($arr_mails as $m) {
                    wp_mail($m,$em_title,$em_content);
                }
                echo 'submit success';
            }
            else
            {
                echo 'store fail';
            }
    }
    else{
        echo 'captcha fail';
    }
    die();
}

?>